//
//  LoginNormalCell.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginNormalCell.h"

#import "MeConfigUI.h"

@interface LoginNormalCell () <RbTextClearViewDelegate>

@property (nonatomic,strong) UIImageView *iconImg;
@property (nonatomic,strong) UIView *verSepView;
@property (nonatomic,strong) RbTextClearView *clearView;




@end


@implementation LoginNormalCell

+ (instancetype)loginNormalCell:(UITableView *)tableView{
    
    LoginNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:LOGINNORMALCELL_ID];
    if (cell == nil) {
        cell = [[LoginNormalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LOGINNORMALCELL_ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:0.1];
        self.layer.cornerRadius = ME_RADIUS;
        self.layer.masksToBounds = YES;
        [self setUpSubViews];
        [self updateConstraintsIfNeeded];
    }
    return self;
}

- (void)setUpSubViews{
    self.iconImg = [[UIImageView alloc] init];
    [self addSubview:self.iconImg];
    
    
    self.userInput = [[UITextField alloc] init];
    [self addSubview:self.userInput];
    self.userInput.attributedPlaceholder = [self configPlaceHolder:@"请输入手机号"];
    [self.userInput setFont:[UIFont systemFontOfSize:LOGIN_FONT_SIZE_MAIN_1]];
    [self.userInput setTextColor:[UIColor colorWithString:LOGIN_COLOR_BG_1]];
    self.userInput.rightViewMode = UITextFieldViewModeWhileEditing;

//    self.userInput.clearButtonMode = UITextFieldViewModeAlways;

    
    self.clearView = [RbTextClearView clearTypeLog];
    self.clearView.frame = CGRectMake(0, 0, RealValueW(30), LOGIN_NORMAL_CELL_HEIGHT);
    self.userInput.rightView = self.clearView;
    self.clearView.delegate = self;

    self.getVerify = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.getVerify];
    [self.getVerify.titleLabel setFont:[UIFont systemFontOfSize:LOGIN_FONT_SIZE_MAIN_1]];
    [self.getVerify setTitleColor:[UIColor colorWithString:LOGIN_COLOR_BG_1] forState:UIControlStateNormal];
    
    [self.getVerify setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.getVerify.hidden = YES;
    
    
    self.verSepView = [UIView new];
    [self addSubview:self.verSepView];
    self.verSepView.backgroundColor = [[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_1];
    self.verSepView.hidden = YES;
}


- (void)updateConstraints{
    [super updateConstraints];
    [self makeConstraints];
}
- (void)makeConstraints{
    
    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(ME_CONSTRAINTS_MARGIN_COMMON_1);
        make.width.height.mas_equalTo(ME_CONSTRAINTS_MARGIN_COMMON_1);
    }];

    [self.userInput mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImg.mas_right).offset(12);
//        make.centerY.equalTo(self.iconImg);
        make.right.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    [self.getVerify mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-ME_CONSTRAINTS_MARGIN_COMMON_1);
        make.centerY.equalTo(self);
    }];

    [self.verSepView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.getVerify.mas_left).offset(-ME_CONSTRAINTS_MARGIN_COMMON_1);
        make.centerY.equalTo(self.getVerify);
        make.height.mas_equalTo(H_LINE_HEIGHT);
        make.width.mas_equalTo(ME_CONSTRAINTS_LINE_WIDTH_COMMON_1);
    }];
}

- (void)setConfigDic:(NSDictionary *)configDic{
    _configDic = configDic;
    self.userInput.attributedPlaceholder = [self configPlaceHolder:configDic[@"title"]];
    self.iconImg.image = [UIImage imageNamed:configDic[@"img"]];
    if (configDic[@"cellType"]) {
        self.getVerify.hidden = NO;
        self.verSepView.hidden = NO;
        self.clearView.hidden = YES;
    }else {
        
    }
}

- (void)setLayoutTouserInput:(NSInteger)layoutTouserInput {
    _layoutTouserInput = layoutTouserInput;
    if (layoutTouserInput == 3) {
        [self.userInput mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.centerY.equalTo(self);
            make.right.equalTo(self);
        }];
    }
}
- (NSAttributedString *)configPlaceHolder:(NSString *)string{
    NSMutableParagraphStyle *style = [self.userInput.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
    style.minimumLineHeight = self.userInput.font.lineHeight - (self.userInput.font.lineHeight - [UIFont systemFontOfSize:LOGIN_FONT_SIZE_MAIN_1].lineHeight) / 2.0;
    
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:string];
    [placeholder addAttributes:@{
                                 NSFontAttributeName : [UIFont systemFontOfSize:LOGIN_FONT_SIZE_MAIN_1],
                                 NSForegroundColorAttributeName :[[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_1],
                                 NSParagraphStyleAttributeName : style
                                 }
                         range:NSMakeRange(0, string.length)];
    return placeholder.copy;
}


- (void)rbTextClearCurrentText:(RbTextClearView *)rbTextClearView{
    self.userInput.text = nil;
    if ([self.loginDelegate respondsToSelector:@selector(clearContent:)]) {
        [self.loginDelegate clearContent:self];
    }
}




@end
